package com.project.class3_30;

import com.lovo.netCRM.component.LovoImageLabel;

import javax.swing.*;

public class ThreadFrame extends JFrame implements Runnable{
    private LovoImageLabel imgLabel = new LovoImageLabel(
            50,50,200,200,this,"image/3.jpg");
    public ThreadFrame(){
        this.setLayout(null);

        this.setSize(800,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);

        //启动线程
        new Thread(this).start();
    }

    public static void main(String[] args) {
        new ThreadFrame();
    }

    private int speed = 5;
    @Override
    public void run() {
        while(true){
            //得到当前图片的坐标
            int x = imgLabel.getX();
            int y = imgLabel.getY();

            if(x > 600 || x<0){
                speed *= -1;
            }

            //设置标签的位置
            imgLabel.setLocation(x + speed,y);

            try {
                Thread.sleep(20);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
